﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Event_Planner
{
    public partial class Event_Organiser : Form
    {
        DB db;
        List<Shop> shopList;
        int selectedShopId, userId = 0;

        public Event_Organiser()
        {
            InitializeComponent();

            db = new DB();
            shopList = getAllShops();
        }

        private void Event_Organiser_Load(object sender, EventArgs e)
        {
            this.cbShops.Items.Add("Select a shop");   //add the first items of the combo boxes
            this.cbShops.SelectedIndex = 0;            //make it the default selected item
            //List<Shop> shops = getAllShops();          //get all the shops
            foreach (Shop shop in shopList)               //go through the list of shops and add their names to the combo box
            {
                this.cbShops.Items.Add(shop.Name);
            }

            //get the sum of balances of all accounts
            DataTable totalbalancesRecords = db.getTotalBalancesFromDB();
            if (totalbalancesRecords.Rows.Count > 0)
            {
                this.txtTotalBalance.Text = totalbalancesRecords.Rows[0]["total"].ToString();
            }

            //get the number of visitors who left
            DataTable visitorsWhoLeft = db.getVisitorsWhoLeftFromDB();
            if (visitorsWhoLeft.Rows.Count > 0)
            {
                this.txtVisitorsLeft.Text = visitorsWhoLeft.Rows[0]["count"].ToString();
            }

            //get the number of current visitors 
            DataTable currentVisitors = db.getCurrentVisitorsFromDB();
            if (currentVisitors.Rows.Count > 0)
            {
                this.txtCurrent.Text = currentVisitors.Rows[0]["count"].ToString();
            }

            //get the number of all visitors 
            DataTable expectedVisitors = db.getAllVisitorsFromDB();
            if (expectedVisitors.Rows.Count > 0)
            {
                this.txtExpected.Text = expectedVisitors.Rows[0]["count"].ToString();
            }

            //get the total sales in every shop
            double totalShopSales = 0;
            DataTable totalShopSalesRecords = db.getTotalShopSalesFromDB();
            if (!(totalShopSalesRecords.Rows[0]["total"] is DBNull))
            {
                totalShopSales = Convert.ToDouble(totalShopSalesRecords.Rows[0]["total"]);
            }

            //get the total amount from hired materials in every stand
            double totalStandSales = 0;
            DataTable totalStandSalesRecords = db.getTotalStandSalesFromDB();
            if (!(totalStandSalesRecords.Rows[0]["total"] is DBNull))
            {
                totalStandSales = Convert.ToDouble(totalStandSalesRecords.Rows[0]["total"].ToString());
            }

            this.txtSpent.Text = (totalShopSales + totalStandSales).ToString();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.txtId.Text == "")
            {
                MessageBox.Show("Enter the Participant's Id");
            }
            else
            {
                int id = Convert.ToInt32(this.txtId.Text);
                if (id > 0)
                {
                    List<Participant> participants = getAllParticipants();
                    foreach (Participant p in participants)
                    {
                        if (p.Number == id)
                        {
                            if (p.PaymentStatus == 0)
                            {
                                this.txtStatus.BackColor = Color.Red;
                                this.txtStatus.Text = "Not Paid";
                            }
                            else
                            {
                                this.txtStatus.BackColor = Color.Green;
                                this.txtStatus.Text = "Paid";
                            }
                            this.txtBalance.Text = p.Balance.ToString();
                        }
                    }
                    //get the total amounts spent in shops by all participants
                    DataTable shopAmountRecords = db.getAmountSpentInShopsFromDB(id);
                    //loop through the records to get the amount spent by this particular participant
                    if (shopAmountRecords != null)
                    {
                        for (int i = 0; i < shopAmountRecords.Rows.Count; i++)
                        {
                            if (Convert.ToInt32(shopAmountRecords.Rows[i]["participant_id"]) == id)
                                this.txtShops.Text = shopAmountRecords.Rows[i]["total"].ToString();
                        }
                    }

                    //get the total amounts transfered by all participants
                    DataTable transferedAmountRecords = db.getAmountTransferedFromDB(id);
                    //loop through the records to get the amount spent by this particular participant
                    if (transferedAmountRecords != null)
                    {
                        for (int i = 0; i < transferedAmountRecords.Rows.Count; i++)
                        {
                            int id2 = Convert.ToInt32(transferedAmountRecords.Rows[i]["id"]);
                            if (Convert.ToInt32(transferedAmountRecords.Rows[i]["id"]) == id)
                                this.txtTransfered.Text = transferedAmountRecords.Rows[i]["total"].ToString();
                        }
                    }

                }
                else
                {
                    MessageBox.Show("Enter a valid Id");
                }
            }
        }

        public List<Participant> getAllParticipants()
        {
            List<Participant> participants = new List<Participant>();
            DataTable participantRecords = db.getAllParticipantsFromDB();
            if (participantRecords != null)
            {
                for (int i = 0; i < participantRecords.Rows.Count; i++)
                {
                    Participant participant = new Participant();
                    participant.Number = Convert.ToInt32(participantRecords.Rows[i]["participant_id"]);
                    participant.FirstName = participantRecords.Rows[i]["first_name"].ToString();
                    participant.LastName = participantRecords.Rows[i]["last_name"].ToString();
                    participant.Email = participantRecords.Rows[i]["email"].ToString();
                    participant.Address = participantRecords.Rows[i]["address"].ToString();
                    participant.DateOfBirth = participantRecords.Rows[i]["date_of_birth"].ToString();
                    participant.RegistrationDate = participantRecords.Rows[i]["registration_date"].ToString();
                    participant.PaymentStatus = Convert.ToInt32(participantRecords.Rows[i]["payment_status"]);
                    participant.AccountNumber = Convert.ToInt32(participantRecords.Rows[i]["account_number"]);
                    participant.Balance = Convert.ToInt32(participantRecords.Rows[i]["balance"]);
                    participants.Add(participant);
                }
            }
            return participants;
        }

        private void button2_Click(object sender, EventArgs e)
        {            
            if (tbPvNo.Text == "")
            {
                MessageBox.Show("Please, fill the private account number.");
            }
            if (this.txtId.Text == "")
            {
                MessageBox.Show("Enter the Participant's Id");
            }
            else
            {
                int id = Convert.ToInt32(this.txtId.Text);
                if (id > 0)
                {                   
                    int accountNumber = db.getUserAccountNumber(id);
                    if (accountNumber > 0)
                    {
                        db.updateParticipantAccount(accountNumber, 0);
                        MessageBox.Show("The total amount was successfully refunded");
                    }
                    else
                    {
                        MessageBox.Show("The provided id is not an existing participant id");
                    }
                }
                else
                {
                    MessageBox.Show("Enter a valid Id");
                }
            }
        }

        public List<Shop> getAllShops()
        {
            List<Shop> shops = new List<Shop>();
            //1. get all shops
            DataTable shopRecords = db.getAllShopsFromDB();
            int shopId;
            string shopName;
            for (int i = 0; i < shopRecords.Rows.Count; i++)
            {
                shopId = Convert.ToInt32(shopRecords.Rows[i]["shop_id"].ToString());
                shopName = shopRecords.Rows[i]["name"].ToString();
                Shop shop = new Shop(shopId, shopName);
                //2. get items for each shop
                shop.Items = getShopItems(shop.Id);
                shops.Add(shop);
            }
            return shops;
        }

        public List<Item> getAllItems()
        {
            List<Item> items = new List<Item>();
            DataTable itemRecords = db.getAllItemsFromDB();
            int itemId;
            string itemName;
            double itemPrice;
            for (int i = 0; i < itemRecords.Rows.Count; i++)
            {
                itemId = Convert.ToInt32(itemRecords.Rows[i]["item_id"].ToString());
                itemName = itemRecords.Rows[i]["name"].ToString();
                itemPrice = Convert.ToDouble(itemRecords.Rows[i]["price"].ToString());
                items.Add(new Item(itemId, itemName, itemPrice));
            }
            return items;
        }

        public List<Item> getShopItems(int shopId)
        {
            List<Item> items = new List<Item>();
            DataTable itemRecords = db.getShopItemsFromDB(shopId);
            int itemId;
            string itemName;
            double itemPrice;
            for (int i = 0; i < itemRecords.Rows.Count; i++)
            {
                itemId = Convert.ToInt32(itemRecords.Rows[i]["item_id"].ToString());
                itemName = itemRecords.Rows[i]["name"].ToString();
                itemPrice = Convert.ToDouble(itemRecords.Rows[i]["price"].ToString());
                items.Add(new Item(itemId, itemName, itemPrice));
            }
            return items;
        }

        private void cbShops_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedShopName = this.cbShops.SelectedItem.ToString();
            //MessageBox.Show(selectedShopName);
            if (selectedShopName != "Select a shop")
            {
                this.txtSalesShops.Text = "0";
                selectedShopId = getShopId(selectedShopName);
                if (selectedShopId != 0)
                {
                    this.cbItems.Items.Clear();
                    List<Item> items = getShopItems(selectedShopId);          //get all the items
                    foreach (Item item in items)               //go through the list of items and add their names to the combo box
                    {
                        this.cbItems.Items.Add(item.Name);
                    }
                    this.cbItems.SelectedIndex = 0;

                    //get the total sales for each shop
                    DataTable shopSalesRecords = db.getSalesPerShopsFromDB();
                    //loop through the records to get the sales for the selected shop
                    for (int i = 0; i < shopSalesRecords.Rows.Count; i++)
                    {
                        if (Convert.ToInt32(shopSalesRecords.Rows[i]["shop_id"]) == selectedShopId)
                            this.txtSalesShops.Text = shopSalesRecords.Rows[i]["total"].ToString();
                    }
                }
            }
        }

        public int getShopId(string shopName)
        {
            for (int i = 0; i < shopList.Count; i++)
            {
                if (shopList[i].Name == shopName)
                {
                    return shopList[i].Id;
                }
            }
            return 0;
        }

        public Item getItem(int shopId, string itemName)
        {
            for (int i = 0; i < shopList.Count; i++)
            {
                if (shopList[i].Id == shopId)
                {
                    for (int j = 0; j < shopList[i].Items.Count; j++)
                    {
                        if (shopList[i].Items[j].Name == itemName)
                        {
                            return shopList[i].Items[j];
                        }
                    }
                }
            }
            return null;
        }

        private void cbItems_SelectedIndexChanged(object sender, EventArgs e)
        {

            string selectedItemName = this.cbItems.SelectedItem.ToString();
            if (selectedItemName != "Select an item")
            {
                this.txtSalesItems.Text = "0";
                if (selectedShopId != 0)
                {
                    //get the item id
                    int itemId = getItem(selectedShopId, selectedItemName).Id;

                    //get the total sales for each shop
                    DataTable itemSalesRecords = db.getSalesPerItemsFromDB();
                    //loop through the records to get the sales for the selected item
                    for (int i = 0; i < itemSalesRecords.Rows.Count; i++)
                    {
                        if (Convert.ToInt32(itemSalesRecords.Rows[i]["item_id"]) == itemId)
                            this.txtSalesItems.Text = itemSalesRecords.Rows[i]["total"].ToString();
                    }
                }
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

